﻿/**********************************************
系统:Ebada物流企业ERP
模块:示例
作者:Rabbit
创建时间:2011-2-28
最后一次修改:2011-2-28
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;
namespace Ebada.Modules.Document
{

    public partial class UCDocumentsB : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<Documents> gridViewOperation;

        public event SendDataEventHandler<Documents> FocusedRowChanged;
        public static bool FirstLoad = false;
        public UCDocumentsB() {
            InitializeComponent();
           
          
            gridViewOperation = new GridViewOperation<Documents>(gridControl1, gridView1, barManager1);
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            Add_columnName();
            Init_Org();
            initColumns();
        }

        void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as Documents);
        }
        //将关联单位标识改为关联单位
        //并以中文显示
        public void Init_Org()
        {
            gridView1.Columns["Org_ID"].Caption = "关联单位";
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["Org_ID"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            comboBox.DataSource = org_list;
            comboBox.DisplayMember = "cnName";
            comboBox.ValueMember = "Org_ID";

        }
        public void Show_Style(string Style)
        {
            if (Style=="借出")
            {
                barBtnMove.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.barButtonItem3.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
            else if (Style == "移出")
            {
                barBtnOut.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
        }
        public void Add_columnName()
        {
            gridView1.Columns["DocumentTypeName"].Caption = "单证种类";
            gridView1.Columns["DocState"].Caption = "单证状态";
           
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["DocState"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            DataTable temptable = new DataTable();
            temptable.Columns.Add("code",typeof(int));
            temptable.Columns.Add("Name",typeof( string));
            DataRow row1=temptable.NewRow();
            DataRow row2=temptable.NewRow();
            DataRow row3=temptable.NewRow();
            DataRow row4=temptable.NewRow();
            row1["code"]=0;
            row1["Name"]="未提交";

            row2["code"]=1;
            row2["Name"]="提交";

            row3["code"]=2;
            row3["Name"]="借出";

            row4["code"]=3;
            row4["Name"]="报废";
            temptable.Rows.Add(row1);
            temptable.Rows.Add(row2);
            temptable.Rows.Add(row3);
            temptable.Rows.Add(row4);


            comboBox.DataSource = temptable;
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "code";
        }
        public void Show_Chezi()
        {
            for (int i = 0; i < 4; i++)
            {
                gridView1.Columns[i].Visible = false;
            }
            int m = 0;
            gridView1.Columns["DocBookID"].Caption = "册子编号";
            gridView1.Columns["DocBookID"].VisibleIndex = m++;
            gridView1.Columns["DocBookName"].Caption = "册子名称";
            gridView1.Columns["DocBookName"].VisibleIndex = m++;
            gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            gridView1.Columns["DocBookSeq"].VisibleIndex = m++;

            gridView1.Columns["OrgName"].Caption = "责任单位名称";
            gridView1.Columns["DocState"].Caption = "单证状态";
            //gridView1.Columns["DocState"].Visible = false;
            //gridView1.Columns["DocStateName"].Caption = "单证状态";
            gridView1.Columns["DocStateName"].Visible = false;
            gridView1.Columns["DocCode"].Caption = "单证编号";
            gridView1.Columns["Act_Date"].Caption = "作业时间";
            gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            gridView1.Columns["CheckResult"].Caption = "审查结果";
            gridView1.Columns["CheckRemark"].Caption = "审查说明";
            gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            gridView1.Columns["CheckUserName"].Caption = "审查人";
            gridView1.Columns["CheckedDate"].Caption = "审查时间";
            gridView1.Columns["Org_ID"].Caption = "承运商";
            gridView1.Columns["SpliteFlag"].Visible = false;
            gridView1.Columns["ScrapDate"].Caption = "报废时间";

           
            gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
        }
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        /// 
       public  void initColumns() 
        {
            ////隐藏单证前3列
            //for (int i = 0; i < 3; i++)
            //{
            //    gridView1.Columns["DocCode"].Visible = false;
            //}
            //gridView1.Columns["DocCode"].Visible = false;
            ////gridView1.Columns["DocumentID"].Visible = false; 
            //gridView1.Columns["DocumentTypeID"].Visible = false;
            //gridView1.Columns["DocumentPositionID"].Visible = false;
            for (int i = 0; i < 4; i++)
            {
                gridView1.Columns[i].Visible = false;
            }
            gridView1.Columns["OrgName"].Caption = "责任单位名称";
            gridView1.Columns["DocState"].Caption = "单证状态";
            //gridView1.Columns["DocState"].Visible = false;
            //gridView1.Columns["DocStateName"].Caption = "单证状态";
            gridView1.Columns["DocStateName"].Visible = false;
            gridView1.Columns["DocCode"].Caption = "单证编号";
            gridView1.Columns["Act_Date"].Caption = "作业时间";
            gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            gridView1.Columns["CheckResult"].Caption = "审查结果";
            gridView1.Columns["CheckRemark"].Caption = "审查说明";
            gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            gridView1.Columns["CheckUserName"].Caption = "审查人";
            gridView1.Columns["CheckedDate"].Caption = "审查时间";
            gridView1.Columns["Org_ID"].Caption = "承运商";
            gridView1.Columns["SpliteFlag"].Visible = false;
            gridView1.Columns["ScrapDate"].Caption = "报废时间";
            gridView1.Columns["DocBookID"].Caption = "册子编号";
            gridView1.Columns["DocBookName"].Caption = "册子名称";
            gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
        }
        //隐藏菜单
       public void Hide_Find()
       {
           this.btFind.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barBtnLq.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barBtnMove.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barBtnOut.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barButtonItem1.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.barButtonItem3.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
       }
       public void Hide_Bar()
       {
           this.bar1.Visible = false;
           this.bar3.Visible = false;
       }
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
        public GridViewOperation<Documents> GridViewOperation {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(Documents newobj) {
            newobj.DocumentID = Guid.NewGuid().ToString();
           
        }
        /// <summary>
        /// 父表ID
        /// </summary>
        [Browsable(false)]
       //根据库位信息刷新单证
        public void RefreshData(List<string> str_list)
        {
            string str = "where 2>1 order by DocState";
            if (str_list.Count>0)
            {
                str = "where DocumentPositionID=";
                for (int i = 0; i < str_list.Count; i++)
                {
                    str += "'" + str_list[i] + "'";
                    str += " or DocumentPositionID=";
                }
                string str2 = " or DocumentPositionID=";
                str = str.Substring(0, str.Length - str2.Length);
                str += "  order by DocState";
            }
            gridViewOperation.RefreshData(str);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.Site == null)
                InitData();
        }
        public void InitData()
        {
            gridViewOperation.RefreshData("Where DocumentID!='' order by DocState");
            if (FirstLoad)
            {
                gridViewOperation.RefreshData("Where DocState=1");
            }
        }
        public void RefreshDataBySqlWhere(string sqlwhere)
        {
            gridViewOperation.RefreshData(sqlwhere);
        }
        public void RefreshData()
        {
            gridViewOperation.RefreshData("Where DocState=1");
        }
        //单证ID
        private string documentID=string.Empty;
        private string DocumentID
        {
            set { documentID = value; }
            get { return documentID; }
        }
        //单证名称
        private string documentName = string.Empty;
        private string DocumentName
        {
            set { documentName = value; }
            get { return documentName; }
        }
        //单证位置
        private string documentPositionID = string.Empty;
        private string DocumentPositionID
        {
            set { documentPositionID = value; }
            get { return documentPositionID; }
        }
        //领取操作
        private void barBtnLq_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DocumentMoveInfo obj = new DocumentMoveInfo();
            obj.DocumentMoveInfoID = obj.CreateID();
            frmDocumentDrawEdit frm = new frmDocumentDrawEdit();
            if (!HaveSelectedRow())
            {
                MessageBox.Show("请先选择单证再进行操作");
                return;
            }
            obj.DocumentID = DocumentID;
            obj.OperateType = "1";
            //领取时间默认为当前
            obj.OutDate = DateTime.Now;
            frm.Operator_Text = "领取单证  " + DocumentName + "  登记信息 ";
            frm.RowData = obj;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MainHelper.PlatformSqlMap.Create<DocumentMoveInfo>(obj);
                    //更改单证状态为借出 
                    Documents obj_Doc = MainHelper.PlatformSqlMap.GetOneByKey<Documents>(DocumentID);
                    obj_Doc.DocState = 2;
                    MainHelper.PlatformSqlMap.Update<Documents>(obj_Doc);
                    RefreshData();
                    MessageBox.Show("操作已完成！");
                }
                catch (Exception ew)
                {
                    MainHelper.ShowException(ew);
                }
            }   
            
        }
        //借出操作
        private void barBtnOut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DocumentMoveInfo obj = new DocumentMoveInfo();
            obj.DocumentMoveInfoID = obj.CreateID();
            frmDocumentBorrowOutEdit frm = new frmDocumentBorrowOutEdit();
            if (!HaveSelectedRow())
            {
                MessageBox.Show("请先选择单证再进行操作");
                return;
            }
            obj.DocumentID = DocumentID;
            obj.OperateType = "领取";
            obj.OutDate = DateTime.Now;
            obj.OutPosition = DocumentPositionID;
            frm.Operator_Text = "借出单证[" + DocumentName + "]登记信息 ";
            frm.RowData = obj;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MainHelper.PlatformSqlMap.Create<DocumentMoveInfo>(obj);
                    //更改单证状态为借出 
                    Documents obj_Doc = MainHelper.PlatformSqlMap.GetOneByKey<Documents>(DocumentID);
                     obj_Doc.DocState = 2;
                     obj_Doc.DocStateName = "借出";
                    MainHelper.PlatformSqlMap.Update<Documents>(obj_Doc);
                    RefreshData();
                    MessageBox.Show("操作已完成！");
                }
                catch (Exception ew)
                {
                    MainHelper.ShowException(ew);
                }
            }   
           
        }
        //移出操作
        private void barBtnMove_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DocumentMoveInfo obj = new DocumentMoveInfo();
            obj.DocumentMoveInfoID = obj.CreateID();
            frmDocumentMoveOutEdit frm = new frmDocumentMoveOutEdit();
            if (!HaveSelectedRow())
            {
                MessageBox.Show("请先选择单证再进行操作");
                return;
            }
            obj.DocumentID = DocumentID;
            obj.OperateType = "移出";
            obj.OutDate = DateTime.Now;
            obj.OutPosition = DocumentPositionID;
            frm.Operator_Text = "移出单证[" + DocumentName + "]登记信息 ";
            frm.RowData = obj;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MainHelper.PlatformSqlMap.Create<DocumentMoveInfo>(obj);
                    //更改单证状态为借出 
                    Documents obj_Doc = MainHelper.PlatformSqlMap.GetOneByKey<Documents>(DocumentID);
                     obj_Doc.DocState = 2;
                     obj_Doc.DocStateName = "借出";
                     obj_Doc.DocumentPositionID = obj.InPosition;
                    MainHelper.PlatformSqlMap.Update<Documents>(obj_Doc);
                    RefreshData();
                    MessageBox.Show("操作已完成！");
                }
                catch (Exception ew)
                {
                    MainHelper.ShowException(ew);
                }
            }   
           
        }
        
       
      
        private bool HaveSelectedRow()
        {
            if (gridView1.FocusedRowHandle> -1)
            {
                DocumentID = gridView1.GetFocusedRowCellValue("DocumentID").ToString();
                DocumentName = gridView1.GetFocusedRowCellValue("DocumentTypeName").ToString();
                DocumentPositionID = gridView1.GetFocusedRowCellValue("DocumentPositionID").ToString();
                return true;
            }
            else
            {
                return false;
            }
           
        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                string type = view.GetRowCellDisplayText(e.RowHandle, view.Columns["DocState"]);
               
                if (type == "0" || type == "未提交")
                {
                    //e.Appearance.BackColor = Color.White;
                }
                if (type == "1" || type == "提交")
                {
                    e.Appearance.BackColor = Color.LightSkyBlue;
                }
                if (type == "2" || type == "借出")
                {
                    e.Appearance.BackColor = Color.LightSeaGreen;
                }
                if (type == "3" || type == "报废")
                {
                    e.Appearance.BackColor = Color.LightGray;
                }
            }
        }
       
        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            //select_list.Clear();
            //for (int i = 0; i < gridView1.GetSelectedRows().Length; i++)
            //{
            //    string tempstr = ((Documents)gridView1.GetRow(gridView1.GetSelectedRows()[i])).VehicleID;
            //    select_list.Add(tempstr);
            //}
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDocumentbookEdit frm = new frmDocumentbookEdit();
            int[] n = gridView1.GetSelectedRows();
            IList<Documents> _doclist = new List<Documents>();
            if (n.Length == 0)
            {
                MessageBox.Show("没有选择单证，不能订册子！");
                return;
            }
            for (int i = 0; i < n.Length; i++)
            {
                Documents doc = gridView1.GetRow(n[i]) as Documents;
                if (doc.DocBookID != "" || doc.DocBookName != "")
                {
                    MessageBox.Show("选择的第" + Convert.ToString(i + 1) + "条单证已经被其他册子关联，请重新选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                doc.DocBookSeq = i + 1;
                _doclist.Add(doc);
            }
            frm.list = _doclist;
            frm.ShowDialog();



        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDocumentbookEdit2 frm = new frmDocumentbookEdit2();
            int[] n = gridView1.GetSelectedRows();
            IList<Documents> _doclist = new List<Documents>();
            if (n.Length==0)
            {
                MessageBox.Show("没有选择单证，不能补单证！");
                return;
            }
            for (int i = 0; i < n.Length; i++)
            {
                Documents doc = gridView1.GetRow(n[i]) as Documents;
                if (doc.DocBookID != "" || doc.DocBookName != "")
                {
                    MessageBox.Show("选择的第" + Convert.ToString(i + 1) + "条单证已经被其他册子关联，请重新选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                doc.DocBookSeq = i + 1;
                _doclist.Add(doc);
            }
            frm.list = _doclist;
            frm.ShowDialog();
        }

        private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.FocusedRowHandle >= 0)
            {
                string type = view.GetRowCellDisplayText(e.FocusedRowHandle, view.Columns["DocState"]);

               
                if (type == "1" || type == "提交")
                {
                    barBtnOut.Enabled = true;
                    barBtnMove.Enabled = true;
                }
                else
	            {
                     barBtnOut.Enabled = false;
                     barBtnMove.Enabled = false;
	            }
                
            }
        }
    }
}
